﻿using NHibernate;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace IceZero.DataStore
{
    public partial class DataStoreBase<T>
    {
        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>主键值(id)</returns>
        public async Task<object> SaveAsync(T obj, CancellationToken cancellationToken = default)
        {
            object id = null;
            await NHibernateHelper.UseSessionWithTransactionAsync(async session =>
            {
                id = await session.SaveAsync(obj, cancellationToken);
            }, cancellationToken);
            return id;
        }

        /// <summary>
        /// 添加对象
        /// </summary>
        /// <param name="obj"></param>
        /// <returns>主键值(id)</returns>
        public async Task<IdType> SaveAsync<IdType>(T obj, CancellationToken cancellationToken = default)
        {
            IdType id = default;
            await NHibernateHelper.UseSessionWithTransactionAsync(async session =>
            {
                id = (IdType)await session.SaveAsync(obj, cancellationToken);
            }, cancellationToken);
            return id;
        }
    }
}
